<template>
<div>
  <el-card shadow="never">
    <el-form :inline="true">
      <el-button type="warning" style="margin: 0 10px" @click="dialogFormVisible = true">添加团长
      </el-button>
      <el-form-item label="团长编号">
        <el-input v-model="searchId" placeholder="请输入团长编号进行查询"/>
      </el-form-item>
      <el-button type="warning" @click="resetData()">清空输入</el-button>
      <el-button type="warning" @click="fetchData()">查询</el-button>
    </el-form>
  </el-card>
  <el-table ref="multipleTable" v-loading="loading" :data="tableData" tooltip-effect="dark"
            border style="width: 75%;margin: 0 auto" max-height="450px" :header-cell-style="tableHeaderCellStyle">
    <el-table-column prop="id" label="团长编号" width="120" sortable />
    <el-table-column prop="nickName" label="昵称" width="140" />
    <el-table-column prop="username" label="用户名" width="140" />
    <el-table-column prop="name" label="团长姓名" width="140" />
    <el-table-column prop="gender" label="团长性别" width="140" />
    <el-table-column prop="age" label="团长年龄" width="140" sortable />
    <el-table-column prop="location" label="自提点" width="140" />
    <el-table-column label="操作">
      <template v-slot="scope">
        <el-button size="mini" type="warning" @click="deleteLeader(scope.$index, scope.row)">删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-dialog title="添加团长" width="30%" :close-on-click-modal="false" :visible.sync="dialogFormVisible" :before-close="handleClose">
    <el-form :model="form">
      <el-form-item label="团长昵称">
        <el-input v-model="form.nickName"></el-input>
      </el-form-item>
      <el-form-item label="用户名">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="真实姓名">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio v-model="form.gender" label="男">男</el-radio>
        <el-radio v-model="form.gender" label="女">女</el-radio>
      </el-form-item>
      <el-form-item label="年龄">
        <el-input v-model="form.age" type="number"></el-input>
      </el-form-item>
      <el-form-item label="自提点">
        <el-input v-model="form.location" placeholder="设置自提点"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="warning" @click="addLeader">确 定</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
import {loadList,addLeader,deleteLeader} from "@/api/leader";

export default {
  name: "Leader",
  data(){
    return{
      searchId: '',
      loading: false,
      tableData: [],
      dialogFormVisible: false,
      form: {}
    }
  },
  mounted() {
    this.fetchData()
  },
  methods:{
    handleClose(done) {
      this.form={}
      done()
    },
    tableHeaderCellStyle () {
      return 'background-color: #fcf3ec;'
    },
    fetchData(){
      this.loading = true
      loadList({id:this.searchId}).then(res=>{
        this.tableData = res.data
        this.loading = false
      })
    },
    resetData(){
      this.searchId=''
      this.fetchData()
    },
    addLeader(){
      addLeader(this.form).then(res=>{
        this.$message({
          type: 'success',
          message: '添加成功',
          center: true
        })
        this.dialogFormVisible = false
        this.searchId=''
        this.fetchData()
      })
    },
    deleteLeader(index,row){
      deleteLeader({id:row.id}).then(res=>{
        this.$message({
          type: 'success',
          message: '删除成功',
          center: true
        })
        this.searchId=''
        this.fetchData()
      })
    }
  }
}
</script>

<style scoped>

</style>
